package com.perhac.forestdefender.utils;

import pulpcore.CoreSystem;

public class Stopwatch {

    private long startMillis = 0;
    private long startPauseMillis = 0;

    public Stopwatch() {
    }

    public void start() {
	startMillis = CoreSystem.getTimeMillis();
    }

    public void pause() {
	startPauseMillis = CoreSystem.getTimeMillis();
    }

    public void resume() {
	startMillis += (CoreSystem.getTimeMillis() - startPauseMillis);
    }

    public long getMillis() {
	return CoreSystem.getTimeMillis() - startMillis;
    }

    public String toString() {
	long millis = getMillis();
	int seconds = (int) (millis / 1000);
	int hours = (int) Math.floor((double) seconds / 3600);
	int minutes = (int) Math.floor((double) seconds / 60);
	seconds = seconds - (hours * 3600) - (minutes * 60);
	minutes = minutes - (hours * 3600);
	if (hours > 0) {
	    return String.format("%dh : %dm : %ds", new Object[] { hours,
		    minutes, seconds });
	} else {
	    return String
		    .format("%dm : %ds", new Object[] { minutes, seconds });
	}
    }

}
